package com.yijiu.trb;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.yijiu.resource.CacheData;

/**
 * 更多
 */
public class MoreActivity extends BTitleWithUserinfoActivity implements OnClickListener
{
	/**
	 * 标签
	 */
	private static final String TAG = "MoreActivity";
	
	/**
	 * 通知
	 */
	private Button noticeButton;
	
	/**
	 * 给我评分
	 */
	private Button gradeButton;
	
	/**
	 * 帮助中心
	 */
	private Button helpButton;
	
	/**
	 * 反馈
	 */
	private Button feedbackButton;
	
	/**
	 * 关于我们
	 */
	private Button aboutButton;
	
	/**
	 * 注销
	 */
	private Button loginOutButton;
	
	/**
	 * 通知列表
	 */
	private ImageView pushButton;
	
	/**
	 * 推送状态监听对象
	 */
	private boolean isPush;
	
	private long exitTime = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		initAndFresh();
		refresh();
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		refresh();
	}
	
	private void initAndFresh()
	{
		setContentView(R.layout.more_activity_layout);
		initWidget();
		if (CacheData.LOGIN_STATUS)
		{
			loginOutButton.setBackgroundResource(R.drawable.register_background);
			loginOutButton.setText("注销");
			loginOutButton.setTextColor(getResources().getColor(R.color.color_three));
		}
		else
		{
			loginOutButton.setBackgroundResource(R.drawable.login_background);
			loginOutButton.setText("登录");
			loginOutButton.setTextColor(getResources().getColor(R.color.white));
		}
	}
	
	@Override
	protected void initWidget()
	{
		super.initWidget();
		pushButton = (ImageView) findViewById(R.id.push);
		noticeButton = (Button) findViewById(R.id.notice);
		gradeButton = (Button) findViewById(R.id.grade);
		helpButton = (Button) findViewById(R.id.help);
		feedbackButton = (Button) findViewById(R.id.feedback);
		aboutButton = (Button) findViewById(R.id.about);
		loginOutButton = (Button) findViewById(R.id.login_out);
		pushButton.setOnClickListener(this);
		noticeButton.setOnClickListener(this);
		gradeButton.setOnClickListener(this);
		helpButton.setOnClickListener(this);
		feedbackButton.setOnClickListener(this);
		aboutButton.setOnClickListener(this);
		loginOutButton.setOnClickListener(this);
		// 推送按钮初始状态打开
		pushButton.setImageResource(R.drawable.push_on);
		isPush = true;
		setTitleInfoVisibility();
	}
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.push:// 消息推送
				if (isPush)
				{
					pushButton.setImageResource(R.drawable.push_off);
					isPush = false;
					Toast.makeText(this, "推送消息已关闭", Toast.LENGTH_SHORT).show();
				}
				else
				{
					pushButton.setImageResource(R.drawable.push_on);
					isPush = true;
					Toast.makeText(this, "推送消息已打开", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.notice:// 通知列表
				Toast.makeText(this, "功能正在开发中", Toast.LENGTH_SHORT).show();
				break;
			case R.id.grade:// 给我评分
				Toast.makeText(this, "功能正在开发中", Toast.LENGTH_SHORT).show();
				break;
			case R.id.help:// 帮助中心
				initIntent(this, HelperActivity.class);
				break;
			case R.id.feedback:// 反馈
				initIntent(this, FeedbackActivity.class);
				break;
			case R.id.about:// 关于我们
				initIntent(this, AboutActivity.class);
				break;
			case R.id.login_out:// 注销
				loginOutButton.setBackgroundResource(R.drawable.login_background);
				loginOutButton.setText("登录");
				loginOutButton.setTextColor(getResources().getColor(R.color.white));
				CacheData.LOGIN_STATUS = false;
				initAndFresh();
				Intent intent = new Intent(this, SuggestProjectActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				System.exit(0);
				break;
			default:
				break;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
		{
			if ((System.currentTimeMillis() - exitTime) > 2000)
			{
				Toast.makeText(getApplicationContext(), "再按一次返回键退出程序", Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			}
			else
			{
				finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
